import { getBaseUrl } from '@/services/urlconst';
import { getUserID, requestFunComponFiex } from '@/utils';
import { useMemo } from 'react';
import { useRequest } from 'umi';

const baseUrl = getBaseUrl();

/**
 * 获取详情
 * @param callback  获取详情回调
 * @returns
 */
const getApplyByIDService = (callback: (res: any) => void) => {
  const getApplyByID = useRequest(
    (params: any) => {
      let reqUrl = `${baseUrl}/purchaseApplyController/getApplyDetails/${params.id}/${getUserID()}`;
      return requestFunComponFiex('GET', reqUrl, {});
    },
    {
      manual: true,
      onSuccess: callback,
    },
  );

  return {
    getApplyByIDRun: getApplyByID?.run,
    getApplyByIDLoading: getApplyByID?.loading,
    getApplyByIDInfo: getApplyByID?.data?.obj,
    reloadDetailInfo: getApplyByID?.refresh,
  };
};

/**
 * 申请撤销
 * @param callback 接口回调
 * @returns
 */
const getApplyinvalidService = (callback: (res: any) => void) => {
  const getApplyinvalid = useRequest(
    (payload: any) => {
      let bodyParams = {
        ...payload,
      };
      let reqUrl = `${baseUrl}/purchaseApplyController/invalid`;
      return requestFunComponFiex('PUT', reqUrl, bodyParams);
    },
    {
      manual: true,
      onSuccess: callback,
    },
  );
  return {
    getApplyinvalidRun: getApplyinvalid?.run,
    getApplyinvalidLoading: getApplyinvalid?.loading,
  };
};

/**
 * 查看当前流程进度
 * @controller 业务controller
 * @returns
 */
const getImageByProcInstIDService = (callBack: (res: any) => void) => {
  const getImageByProcInstID = useRequest(
    (params: any) => {
      let personUrl = `${baseUrl}/purchaseApplyController/getImageByProcInstID/${params?.procInstID}`;
      return requestFunComponFiex('GET', personUrl, {});
    },
    {
      manual: true,
      onSuccess: callBack,
    },
  );

  const data = useMemo(() => {
    return getImageByProcInstID?.data;
  }, [getImageByProcInstID?.data]);

  return {
    getImageByProcInstIDRun: getImageByProcInstID?.run,
    getImageByProcInstIDLoading: getImageByProcInstID?.loading,
    getImageByProcInstIDData: data,
  };
};

export { getApplyByIDService, getApplyinvalidService, getImageByProcInstIDService };
